// CPage10_Champ.cpp: Implementierungsdatei
//

#include "stdafx.h"
#include "CharplanDoc.h"
#include "Charplan.h"
#include "CPage10_Champ.h"


// CPage10_Champ-Dialogfeld

IMPLEMENT_DYNAMIC(CPage10_Champ, CAPage)

CPage10_Champ::CPage10_Champ(CWnd* pParent /*=NULL*/)
	: CAPage(CPage10_Champ::IDD, pParent)
{

}

CPage10_Champ::~CPage10_Champ()
{
}

void CPage10_Champ::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST2, mwnd_champlist);
	DDX_Control(pDX, IDC_DETAILS, mwnd_details);
	DDX_Control(pDX, IDC_EDIT1, mwnd_champlevel);
}


BEGIN_MESSAGE_MAP(CPage10_Champ, CAPage)
	ON_MESSAGE(MLN_ITEM_HILITE,OnItemHilite)
	ON_MESSAGE(MLN_ITEM_CLICK,OnItemClick)
	ON_WM_SIZE()
END_MESSAGE_MAP()


BOOL CPage10_Champ::OnInitDialog()
{
	CAPage::OnInitDialog();
	
	mwnd_champlevel.SetColorsDefault();

	OnClassChanged();

	return TRUE;
}


void CPage10_Champ::OnClassChanged()
{
	mwnd_champlist.DeleteContents();
	mwnd_details.SetWindowText(_T(""));


	// get Champ trainers
	CStringArray	trainers;
 	IntArray		trainer_lines;

	gdbinterface.GetListOfChampTrainers(trainers,trainer_lines);

	COLORREF col = 0;

	for (int tra=0;tra<trainers.GetCount();tra++)
		for (int tl=0;tl<trainer_lines[tra];tl++)
	{
		// get champ_trainer_lines
		IntArray		spells;
		gdbinterface.GetListOfChampSpells(trainers[tra],tl,spells);


		// get spells
		int line = (int)(mwnd_champlist.AddLine(trainers[tra]));
		for (int sp=0;sp<spells.GetCount();sp++)
		{
			CString name;
			CString type;
			CStringArray values;
			int colid;
 			gdbinterface.GetChampSpell(spells[sp],name,type,values,colid);

			{
				CString col_name;
				col_name.Format(_T("COLOR_%02i"),colid);
				col = GETOPTION(col_name);
			}

			if (type[1]=='p')
			{
				// type + effect
				mwnd_champlist.AddText(line,values[0],values[5],col,(tra<<8)|(tl<<4)|sp);
			}
			else
			{
				// "Style" + pre
				mwnd_champlist.AddText(line,type,values[1],col,(tra<<8)|(tl<<4)|sp);
			}
		}
	}


	// M.Jurkait [2/6/2006]
	// -------------------------
	// set lits
	// -------------------------
	int n = GetDocument()->GetNofCamplevel();
	for (int t=0;t<n;t++)
	{
		int trainer,pfad,lvl;
		GetDocument()->GetChamplevel(t,trainer,pfad,lvl);

		// find skill
		// get Champ trainers
		CStringArray	trainers;
		IntArray		trainer_lines;
		gdbinterface.GetListOfChampTrainers(trainers,trainer_lines);

		if (trainer < trainers.GetSize() && pfad<trainer_lines[trainer])
		{
			int l = pfad;
			for (int tr=0;tr<trainer;tr++) l += trainer_lines[tr];

			mwnd_champlist.SetChecked(l, lvl);
		}
		else
		{
			ASSERT(false);
			TRACE("Found illegal Champ-spell");
			GetDocument()->RemoveChamplevel(trainer,pfad,lvl);
			t--;n--;
		}
	}

	PointsChanged();
}


//////////////////////////////////////////////////////////////////////////
LRESULT CPage10_Champ::OnItemHilite(WPARAM w, LPARAM)
{
	int l,s;
	l = LOWORD(w);
	s = HIWORD(w);

	if (l!=0xffff && s!=0xffff)
	{
		int data = mwnd_champlist.GetItemData(l,s);
		int tra = (data>>8)&0xf;
		int tl = (data>>4)&0xf;
		int sp = data&0xf;


		CStringArray	trainers;
		IntArray		trainer_lines;
		IntArray		spells;
		gdbinterface.GetListOfChampTrainers(trainers,trainer_lines);
		gdbinterface.GetListOfChampSpells(trainers[tra],tl,spells);

		// get spells
		CString name;
		CString type;
		CStringArray values;
		int	colid;
		gdbinterface.GetChampSpell(spells[sp],name,type,values,colid);

		CString text;
		if (type[1]=='p')
		{
			text += CString(_T("Name:  ")) + name + _T("\n\r");
			text += CString(_T("Type:   ")) + values[0] + _T("\n\r");
			text += CString(_T("Target: ")) + values[1] + _T("\n\r");
			text += CString(_T("C/D/R: ")) + values[2] + _T("\n\r");
			text += CString(_T("Range: ")) + values[3] + _T("\n\r");
			text += CString(_T("Cost: ")) + values[4] + _T("\n\r");
			text += CString(_T("Effect: ")) + values[5] + _T("\n\r");
		}
		else
		{
			text += _T("STYLE\n\r");
			text += CString(_T("Name: ")) + name + _T("\n\r");
			text += CString(_T("Weapon:   ")) + values[0] + _T("\n\r");
			text += CString(_T("Preq.: ")) + values[1] + _T("\n\r");
			text += CString(_T("To-Hit: ")) + values[2] + _T("\n\r");
			text += CString(_T("Defense: ")) + values[3] + _T("\n\r");
			text += CString(_T("Fatigue: ")) + values[4] + _T("\n\r");
			text += CString(_T("Damage: ")) + values[5] + _T("\n\r");
			text += CString(_T("Effect: ")) + values[6] + _T("\n\r");
		}



		mwnd_details.SetWindowText(text);
	}
	else
		mwnd_details.SetWindowText(_T(""));


	return 0;
}



//////////////////////////////////////////////////////////////////////////
LRESULT CPage10_Champ::OnItemClick(WPARAM w, LPARAM)
{
	int l,s;
	l = LOWORD(w);
	s = HIWORD(w);

	if (l!=0xffff && s!=0xffff)
	{
		int data = mwnd_champlist.GetItemData(l,s);
		int tra = (data>>8)&0xf;
		int tl = (data>>4)&0xf;
		int sp = data&0xf;
		ASSERT(sp==s);

		bool b_add = !mwnd_champlist.GetChecked(l,s);

		if (b_add)
		{
			for (int l1=0;l1<=sp;l1++)
				if (!mwnd_champlist.GetChecked(l,l1))
				{
					GetDocument()->AddChamplevel(tra,tl,l1);
					mwnd_champlist.SetChecked(l,l1);
				}
		}
		else
		{

			if (sp<4 && mwnd_champlist.GetChecked(l,sp+1))	sp++;

			for (int l1=sp;l1<5;l1++)
				if (mwnd_champlist.GetChecked(l,l1))
				{
					GetDocument()->RemoveChamplevel(tra,tl,l1);
					mwnd_champlist.SetChecked(l,l1,false);
				}
		}


		PointsChanged();
	}

	return 0;
}


void CPage10_Champ::OnChampChanged()	
{
    PointsChanged();
};


void CPage10_Champ::PointsChanged()
{
	CString temp;
	temp.Format(_T("Champ-Level: %i"),GetDocument()->GetNofCamplevel());
	mwnd_champlevel.SetWindowText(temp);

	if (GetDocument()->GetNofCamplevel() > GetDocument()->GetCHlevel())
		mwnd_champlevel.SetTextColor(RGB(255,64,64));
	else
		mwnd_champlevel.SetColorsDefault();

}


void CPage10_Champ::OnSize(UINT nType, int cx, int cy)
{
	CAPage::OnSize(nType, cx, cy);

	if (!IsWindow(mwnd_champlist.m_hWnd)) return;

	CRect rect2;
	mwnd_details.GetWindowRect(rect2);
	ScreenToClient(rect2);

	CRect rect;
	mwnd_champlist.GetWindowRect(rect);
	ScreenToClient(rect);
	mwnd_champlist.SetWindowPos(NULL, 0,0,
						cx-rect2.Width()-30,cy-2*rect.top,
						SWP_NOZORDER | SWP_NOACTIVATE |SWP_NOMOVE  );

	mwnd_details.SetWindowPos(NULL, cx-rect2.Width()-10,rect2.top,
								0,0,
								SWP_NOZORDER | SWP_NOACTIVATE |SWP_NOSIZE  );


	mwnd_champlevel.GetWindowRect(rect);
	ScreenToClient(rect);
	mwnd_champlevel.SetWindowPos(NULL, cx-rect2.Width()-10,rect.top,
								0,0,
								SWP_NOZORDER | SWP_NOACTIVATE |SWP_NOSIZE  );

}
